package com.tony.firewall.common;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.os.Environment;

public class MessageFormatter {
	private static String FILE_DIR = ".message";
	private static String FILE_SUFFIX_XML = ".xml";
	private static String CONTENT_FORMATTER = "<sms>\r\n" + "<id>%s</id>\r\n" + "<address>%s</address>\r\n" + "<name>%s</name>\r\n" + "<body>%s</body>\r\n" + "<time>%s</time>\r\n" + "</sms>";

	private static File getRoot() {
		File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + FILE_DIR);
		if (!root.exists()) {
			root.mkdirs();
		}
		return root;
	}

	public static String formatContent(long id, String address, String name, String body, long time) {
		String format = String.format(CONTENT_FORMATTER, id, address, name, body, time);
		return format;
	}

	public static void writeToFile(final String msg) {
		File file = new File(getRoot(), "message-" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) + FILE_SUFFIX_XML);
		FileUtil.writeToFile(file, msg, false);
	}

}
